#!/bin/bash

VERSION="NaNoWriMo goal calculator 1.1.1"

E_WRONG_PARAM=1
E_NO_STORY=2
E_INVAL_DAY=3

DAY=$(date +%d)
MONTH=$(date +$m)
YEAR=$(date +%Y)

CURR=0
GOAL=50000

MLEN=
NDAY=
NTIME=0
SFILE=
SGOAL=
STORY=()
SWORD=0
TDAY=

function usage
{
	echo "Usage:"
	echo "  ./word_goal.sh [OPTION...] - Calculate per-day word goals"
	echo -e "\n  -h, --help          Shows this list of options."
	echo -e "\nCount Options"
	echo -e "  -c, --count <count> Sets the word count to <count>, unless -f is also set."
	echo "  -f, --file   <file> Attempts to count and use the word count of <file>."
	echo "  -g, --goal   <goal> Changes the ending word goal from 50,000 to <goal>."
	echo -e "  -s, --short-day     Displays the actual number of words remaining, even if\n                        that is lower than the base number per day."
	echo -e "\nTime Options"
	echo -e "    -d and -m expect two-digit format (e.g. 06)\n    -y only requires four digits when given a century (e.g. 2100)."
	echo "  -d, --date   <date> Uses <date> in determining the time left."
	echo "  -e, --end-of-day    Calculates as if run after finishing the day's writing."
	echo "  -m, --month <month> Uses <month> in obtaining the total number of days."
	echo -e "  -n, --days <length> Uses <length> as the number of days remaining.\n                        Overrides any calculations based on the date."
	echo "  -y, --year   <year> Uses <year> in deciding February's length."
}

while [ -n "$1" ] ; do
	case $1 in
		-c | --count )		shift
					CURR=$1
					;;
		-g | --goal )		shift
					GOAL=$1
					;;
		-f | --file )		shift
					SFILE=$1
					;;
		-n | --days )		shift
					NDAY=$1
					NTIME=1
					;;
		-d | --date )		shift
					DAY=$1
					;;
		-m | --month )		shift
					MONTH=$1
					;;
		-y | --year )		shift
					YEAR=$1
					;;
		-e | --end-of-day )	DAY=$(( DAY + 1 ))
					;;
		-s | --short-day )	SWORD=1
					;;
		-h | --help )		usage
					exit 0
					;;
		-V | --version )	echo "$VERSION"
					exit 0
					;;
		* )			usage
					exit $E_WRONG_PARAM
	esac
	shift
done

if [ "$SFILE" ] ; then
	if [ ! -s "$SFILE" ] ; then
		echo "$SFILE does not exist or is empty."
		exit $E_NO_STORY
	fi
	STORY=$(cat "$SFILE")
	CURR=$(wc -w "$SFILE")
	set -- $CURR
	CURR=$1
	echo "$CURR words found in $SFILE."
fi

if [ !"$NDAY" ] ; then # If NDAY was not set manually
	case $MONTH in
	# Leap years only occur on the century if the year is divisible by four hundred
		02 )			if [ $(( YEAR % 100 )) -eq 0 ] ; then
						if [ $(( YEAR % 400 )) -eq 0 ] ; then
							MLEN=29
						else
							MLEN=28
						fi
					elif [ $(( YEAR % 4 )) -eq 0 ] ; then
						MLEN=29
					else
						MLEN=28
					fi
					;;
		04 | 06 | 09 | 11 )	MLEN=30
					;;
		* )			MLEN=31
	esac
	if [ $DAY -lt 1 -o $DAY -gt $MLEN ] ; then
		echo "That date ($DAY) is not valid for the month ($MONTH)."
		exit $E_INVAL_DAY
	fi
	NDAY=$(( MLEN - DAY + 1 ))
fi

SGOAL=$(( GOAL - CURR ))

# DGOAL uses the small total goal (SGOAL) divided by the number of remaining days
DGOAL=$(( SGOAL / NDAY ))
if [ $(( SGOAL % NDAY )) -ne 0 ] ; then
	DGOAL=$(( DGOAL + 1 ))
fi

# TGOAL uses the large total goal (GOAL) divided by the length of the month
if [ $SWORD -ne 1 -a $NTIME -ne 1 ] ; then
	TGOAL=$(( GOAL / MLEN ))
	if [ $(( GOAL % MLEN )) -ne 0 ] ; then
		TGOAL=$(( TGOAL + 1 ))
	fi
	if [ $DGOAL -lt $TGOAL ] ; then
		DGOAL=$TGOAL
	fi
fi

echo "For the next $NDAY days, write $DGOAL words."

exit 0
